<template>
	<view class="order-flow">
		<!-- 第一行：从左到右 -->
		<view class="flow-row flow-row-top">
			<block v-for="(step, index) in topRowSteps" :key="index">
				<view class="step-content">
					<view 
						class="step-circle" 
						:class="{ active: step.isActive, completed: step.isCompleted }"
					>
						<text class="step-number">{{ step.stepNumber }}</text>
					</view>
					<text 
						class="step-label" 
						:class="{ active: step.isActive, completed: step.isCompleted }"
					>{{ step.label }}</text>
				</view>
				<view 
					class="step-connector-horizontal" 
					v-if="index < topRowSteps.length - 1"
					:class="{ active: step.isCompleted }"
				></view>
			</block>
		</view>
		
		<!-- 垂直连接线 -->
		<view class="flow-vertical-connector" v-if="bottomRowSteps.length > 0 && isExpanded">
			<view 
				class="vertical-line" 
				:class="{ active: topRowSteps[topRowSteps.length - 1].isCompleted }"
			></view>
		</view>
		
		<!-- 第二行：从右到左 -->
		<view 
			class="flow-row flow-row-bottom" 
			:style="{ justifyContent: bottomRowSteps.length > 3 && isExpanded ? 'space-around' : 'flex-start' }"
			v-if="bottomRowSteps.length > 0 && isExpanded"
		>
			<block v-for="(step, index) in bottomRowSteps" :key="index">
				<view class="step-content">
					<view 
						class="step-circle" 
						:class="{ active: step.isActive, completed: step.isCompleted }"
					>
						<text class="step-number">{{ step.stepNumber }}</text>
					</view>
					<text 
						class="step-label" 
						:class="{ active: step.isActive, completed: step.isCompleted }"
					>{{ step.label }}</text>
				</view>
				<view 
					class="step-connector-horizontal" 
					v-if="index < bottomRowSteps.length - 1"
					:class="{ active: step.isCompleted }"
				></view>
			</block>
		</view>
		
		<!-- 收起/展开按钮 -->
		<view 
			class="expand-toggle" 
			v-if="bottomRowSteps.length > 0"
			@click="toggleExpand"
		>
			<view class="expand-icon" :class="{ expanded: isExpanded }"></view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'OrderFlowChart',
	props: {
		// 订单类型：1-普通订单流程，2-入库流程
		orderType: {
			type: [Number, String],
			default: 1
		},
		// 订单状态：1-待发货，2-已发货/待收货，3-已完成，4-已关闭（orderType=1时）
		// 入库状态：1-待入库，2-已入库（orderType=2时）
		orderStatus: {
			type: [Number, String],
			default: null
		},
		// 支付状态：0-未支付，1-已支付，2-补交中
		payStatus: {
			type: [Number, String],
			default: null
		},
		// 支付方式：如"在线支付"、"微信支付"、"欠款"等，可以是字符串或数字
		payMethod: {
			type: [String, Number],
			default: ''
		}
	},
	data() {
		return {
			isExpanded: true // 默认展开第二行
		}
	},
	mounted() {
		console.log('组件mounted - orderType:', this.orderType)
	},
	watch: {
		orderType(newVal) {
			console.log('orderType变化:', newVal)
		}
	},
	methods: {
		// 切换展开/收起状态
		toggleExpand() {
			this.isExpanded = !this.isExpanded
		}
	},
	computed: {
		// 根据订单状态计算当前流程步骤
		currentFlowSteps() {
			if (this.orderStatus === null || this.orderStatus === undefined) {
				return []
			}
			
			const orderType = Number(this.orderType) || 1
			const orderStatus = this.orderStatus
			const payStatus = this.payStatus
			// 统一处理 payMethod，支持字符串和数字
			const payMethod = this.payMethod !== null && this.payMethod !== undefined ? String(this.payMethod) : ''
			console.log('orderType prop:', this.orderType, 'parsed:', orderType, 'orderStatus:', orderStatus, 'payStatus:', payStatus, 'payMethod:', payMethod);
			
			// 如果订单类型是2，显示入库流程
			if (orderType == 2) {
				// 入库流程：待入库 -> 已入库
				const steps = [
					{ label: '待入库', key: 'pending_stock' },
					{ label: '已入库', key: 'stocked' },
					{ label: '待交款', key: 'pending_pay' },
					{ label: '交款中', key: 'repaying' },
					{ label: '已交款', key: 'paid' },
					{ label: '已完成', key: 'completed' }
				]
				
				// 计算每个步骤的状态
				// 注意：实际数据中 orderStatus == 5 表示已入库
				let mappedOrderStatus = orderStatus
				let currentStepIndex = -1
				if (mappedOrderStatus == 1) {
					// 待入库（步骤1）
					currentStepIndex = 0
				} else if (mappedOrderStatus == 5 && payStatus == 0) {
					// 已入库（步骤2）
					currentStepIndex = 2
				} else if (mappedOrderStatus == 3 && payStatus == 1) {
					// 交款中（步骤4）（步骤5）
					currentStepIndex = 6
				} else if (mappedOrderStatus == 5 && payStatus == 2) {
					// 已交款（步骤5）
					currentStepIndex = 4
				}
				
				// 标记每个步骤的状态
				return steps.map((step, index) => {
					let isCompleted = index < currentStepIndex
					let isActive = index === currentStepIndex
					
					return {
						...step,
						stepNumber: index + 1,
						isCompleted,
						isActive
					}
				})
			}
			
			// 普通订单流程（orderType == 1）
			// 根据支付方式判断显示哪种流程图
			// 如果支付方式为0或"0"，显示已付款流程图，否则显示未付款流程图
			const isUnpaidFlow = payMethod != 0 && payMethod != '0'
			
			// 已付款流程
			let steps = []
			if (!isUnpaidFlow) {
				// 已付款流程：已付款 -> 待发货 -> 已发货 -> 待收货 -> 已完成
				steps = [
					{ label: '已付款', key: 'paid' },
					{ label: '待发货', key: 'pending_ship' },
					{ label: '已发货', key: 'shipped' },
					{ label: '待收货', key: 'pending_receive' },
					{ label: '已完成', key: 'completed' }
				]
			} else {
				// 未付款流程：未付款 -> 待发货 -> 已发货 -> 待付款 -> 补交中 -> 已审核 -> 待收货 -> 已完成
				steps = [
					{ label: '未付款', key: 'unpaid' },
					{ label: '待发货', key: 'pending_ship' },
					{ label: '已发货', key: 'shipped' },
					{ label: '待付款', key: 'pending_pay' },
					{ label: '补交中', key: 'repaying' },
					{ label: '已审核', key: 'audited' },
					{ label: '待收货', key: 'pending_receive' },
					{ label: '已完成', key: 'completed' }
				]
			}
			
			// 计算每个步骤的状态
			let currentStepIndex = -1
			
			if (!isUnpaidFlow) {
				// 已付款流程：已付款 -> 待发货 -> 已发货 -> 待收货 -> 已完成
				if (orderStatus == 1) {
					// 待发货（步骤2）
					currentStepIndex = 1
				} else if (orderStatus == 2) {
					// 待收货（步骤4），因为已发货（步骤3）已经完成
					currentStepIndex = 3
				} else if (orderStatus == 3) {
					// 已完成（步骤5）
					currentStepIndex = 5
				}
			} else {
				// 未付款流程：未付款 -> 待发货 -> 已发货 -> 待付款 -> 补交中 -> 已审核 -> 待收货 -> 已完成
				if (orderStatus == 1) {
					if (payStatus == 0) {
						// 待发货（步骤2）
						currentStepIndex = 1
					}
				} else if (orderStatus == 2) {
					if (payStatus == 0) {
						// 待付款（步骤4）
						currentStepIndex = 3
					} else if (payStatus == 2) {
						// 补交中（步骤5）
						currentStepIndex = 4
					} else if (payStatus == 1) {
						// 已审核通过，待收货（步骤7），已审核（步骤6）已完成
						currentStepIndex = 6
					}
				} else if (orderStatus == 3) {
					// 已完成（步骤8）
					currentStepIndex = 8
				}
			}
			
			// 标记每个步骤的状态
			return steps.map((step, index) => {
				let isCompleted = index < currentStepIndex
				let isActive = index === currentStepIndex
				
				if (!isUnpaidFlow) {
					// 已付款流程的特殊处理
					// 已付款步骤（index=0）总是完成的
					if (index === 0) {
						isCompleted = true
						isActive = false
					}
					// 如果已发货（orderStatus >= 2），已发货步骤（index=2）已完成
					if (orderStatus >= 2 && index === 2) {
						isCompleted = true
						isActive = false
					}
				} else {
					// 未付款流程的特殊处理
					// 如果已发货（orderStatus >= 2），待发货（index=1）和已发货（index=2）步骤已完成
					if (orderStatus >= 2) {
						if (index === 1 || index === 2) {
							isCompleted = true
							isActive = false
						}
					}
					// 如果已审核通过（orderStatus == 2 && payStatus == 1），已审核步骤（index=5）已完成
					if (orderStatus == 2 && payStatus == 1 && index === 5) {
						isCompleted = true
						isActive = false
					}
				}
				
				return {
					...step,
					stepNumber: index + 1,
					isCompleted,
					isActive
				}
			})
		},
		// 第一行步骤（从左到右）
		topRowSteps() {
			const steps = this.currentFlowSteps
			if (steps.length == 2) {
				// 入库流程：全部2个步骤都在第一行
				return steps
			} else if (steps.length <= 5) {
				// 已付款流程：前4个步骤
				return steps.slice(0, 4)
			} else {
				// 未付款流程：前4个步骤
				return steps.slice(0, 4)
			}
		},
		// 第二行步骤（从右到左，使用row-reverse来实现反转）
		bottomRowSteps() {
			const steps = this.currentFlowSteps
			if (steps.length == 2) {
				// 入库流程：没有第二行
				return []
			} else if (steps.length <= 5) {
				// 已付款流程：后1个步骤，保持原顺序，使用CSS row-reverse实现从右到左显示
				return steps.slice(4)
			} else {
				// 未付款流程：后4个步骤，保持原顺序，使用CSS row-reverse实现从右到左显示
				return steps.slice(4)
			}
		}
	}
}
</script>

<style scoped>
/* 订单流程图 */
.order-flow {
	padding: 15px 10px;
	background-color: #ffffff;
	border-bottom: 1px solid #f0f0f0;
	overflow-x: auto;
}

/* 流程行 */
.flow-row {
	display: flex;
	align-items: flex-start;
	justify-content: flex-start;
	position: relative;
	flex-wrap: nowrap;
}
.flow-row-top {
	justify-content: space-around;
}
/* 第二行从右到左排列 */
.flow-row-bottom {
	flex-direction: row-reverse;
}

/* 步骤内容 */
.step-content {
	display: flex;
	flex-direction: column;
	align-items: center;
	position: relative;
	z-index: 2;
	flex-shrink: 0;
	min-width: 45px;
	max-width: 45px;
}

/* 步骤圆形 */
.step-circle {
	width: 28px;
	height: 28px;
	border-radius: 50%;
	background-color: #e0e0e0;
	display: flex;
	align-items: center;
	justify-content: center;
	border: 2px solid #e0e0e0;
	box-sizing: border-box;
	transition: all 0.3s;
	flex-shrink: 0;
}

.step-circle.active {
	background-color: #ff4069;
	border-color: #ff4069;
}

.step-circle.completed {
	background-color: #4CAF50;
	border-color: #4CAF50;
}

/* 步骤编号 */
.step-number {
	font-size: 12px;
	color: #999999;
	font-weight: bold;
}

.step-circle.active .step-number,
.step-circle.completed .step-number {
	color: #ffffff;
}

/* 步骤标签 */
.step-label {
	font-size: 10px;
	color: #999999;
	margin-top: 4px;
	text-align: center;
	line-height: 1.2;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	width: 100%;
}

.step-label.active {
	color: #ff4069;
	font-weight: bold;
}

.step-label.completed {
	color: #4CAF50;
}

/* 水平连接线 */
.step-connector-horizontal {
	flex: 1;
	height: 2px;
	background-color: #e0e0e0;
	margin: 0 2px;
	position: relative;
	top: 14px;
	z-index: 1;
	min-width: 15px;
	max-width: 40px;
}

.step-connector-horizontal.active {
	background-color: #4CAF50;
}

/* 垂直连接线容器 */
.flow-vertical-connector {
	display: flex;
	justify-content: flex-end;
	align-items: center;
	padding: 20px 7px;
	position: relative;
	height: 25px;
}

/* 垂直连接线 */
.vertical-line {
	width: 2px;
	height: 25px;
	background-color: #e0e0e0;
	margin-right: 14px;
}

.vertical-line.active {
	background-color: #4CAF50;
}

/* 收起/展开按钮 */
.expand-toggle {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 8px 0;
	cursor: pointer;
}

.expand-icon {
	width: 0;
	height: 0;
	border-left: 6px solid transparent;
	border-right: 6px solid transparent;
	border-top: 8px solid #999999;
	transition: all 0.3s ease;
	display: inline-block;
}

.expand-icon.expanded {
	border-top: none;
	border-bottom: 8px solid #999999;
}
</style>
